home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
Jul
/
di9807rl
/
viewform.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1998-02-26
|
4KB
|
155 lines
unit ViewForm;
{ Simple program that displays the current system palette.
Copyright ⌐ 1998 Tempest Software, Inc.
}
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls;
type
TPaletteForm = class(TForm)
ErrorLabel: TLabel;
StatusBar: TStatusBar;
ColorPanel: TPaintBox;
procedure ColorPanelPaint(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ColorPanelMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
fNumColors: Word;
fPalette: HPalette;
protected
function GetPalette: HPalette; override;
property Palette: HPalette read fPalette;
property NumColors: Word read fNumColors;
end;
var
PaletteForm: TPaletteForm;
implementation
{$R *.DFM}
// Set up the form's palette to display the system palette.
procedure TPaletteForm.FormCreate(Sender: TObject);
var
LogPal: PLogPalette;
Size: LongInt;
I: Word;
begin
if (GetDeviceCaps(Canvas.Handle, RasterCaps) and Rc_Palette) = 0 then
begin
ErrorLabel.Visible := True;
Exit;
end;
fNumColors := GetDeviceCaps(Canvas.Handle, SizePalette);
Size := SizeOf(TLogPalette) + LongInt(NumColors-1) * SizeOf(TPaletteEntry);
GetMem(LogPal, Size);
LogPal^.palVersion := $300;
LogPal^.palNumEntries := NumColors;
for I := 0 to NumColors-1 do
begin
{$R-}
LongInt(LogPal^.palPalEntry[I]) := I;
LogPal^.palPalEntry[I].peFlags := Pc_Explicit;
{$R+}
end;
fPalette := CreatePalette(LogPal^);
if fPalette = 0 then
raise EOutOfResources.Create('Cannot create palette');
end;
// Free the palette when the form closes
procedure TPaletteForm.FormDestroy(Sender: TObject);
begin
if fPalette <> 0 then
DeleteObject(fPalette);
fPalette := 0;
end;
// Return the form's palette.
function TPaletteForm.GetPalette: HPalette;
begin
if fPalette <> 0 then
Result := fPalette
else
Result := inherited GetPalette
end;
// Paint the form with a grid of boxes, where each box is filled
// with a palette color. To make it easier to see the colors,
// leave a small margin around each box and around the edge of
// the form.
procedure TPaletteForm.ColorPanelPaint(Sender: TObject);
const
NCols = 16;
Margin = 2;
var
I: Integer;
X, Y: Integer; // top, left corner of the next box
W, H: Integer; // size of each box
NRows: Integer;
OldPal: HPalette;
begin
// Non-palette device?
if Palette = 0 then
Exit;
OldPal := SelectPalette(ColorPanel.Canvas.Handle, Palette, False);
try
ColorPanel.Canvas.Pen.Color := clBlack;
NRows := (LongInt(NumColors)+NCols-1) div NCols;
W := ColorPanel.ClientWidth div NCols - Margin;
H := ColorPanel.ClientHeight div NRows - Margin;
X := 1;
Y := 1;
for I := 0 to NumColors-1 do
begin
ColorPanel.Canvas.Brush.Color := PaletteIndex(I);
ColorPanel.Canvas.Rectangle(X, Y, X+W, Y+H);
// Determine the position of the next box: does it fit
// on the current line, or start a new row?
if I mod NCols < NCols-1 then
X := X + W + Margin
else
begin
X := 1;
Y := Y + H + Margin;
end;
end;
finally
SelectPalette(ColorPanel.Canvas.Handle, OldPal, True);
end;
end;
// Redraw the palette boxes when the form changes size
procedure TPaletteForm.FormResize(Sender: TObject);
begin
ColorPanel.Invalidate
end;
// Display the color of the pixel under the mouse.
procedure TPaletteForm.ColorPanelMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
Color: LongInt;
begin
Color := ColorPanel.Canvas.Pixels[X, Y];
StatusBar.Panels[0].Text := Format('R: $%2.2X', [GetRValue(Color)]);
StatusBar.Panels[1].Text := Format('G: $%2.2X', [GetGValue(Color)]);
StatusBar.Panels[2].Text := Format('B: $%2.2X', [GetBValue(Color)]);
end;
end.